package com.bwie.product.config;

import com.alibaba.fastjson.JSONObject;
import com.bwie.common.redis.service.RedisService;
import com.bwie.common.security.utils.SecurityUtils;
import com.bwie.product.domain.SkuInfo;
import com.bwie.system.api.model.LoginUser;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Component
public class OrderMq {
    @Autowired
    private RedisService redisService;


    @RabbitListener
    public void sendMg(Integer skuId) {
        //获取当前用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //判断用户是否为空
        if (loginUser == null) {
            throw new RuntimeException("没有获取的用户信息");
        }

        //用户id
        Long userid = loginUser.getUserid();
        //根据用户购物车查询订单
        Collection<String> cartKeys = redisService.keys("CART:" + userid);



        //redis中取数据
        for (String cartKey : cartKeys) {

            //根据购物车键查询购物车详情
            List<SkuInfo> cartItemList = redisService.getMultiCacheMapValue(cartKey, Collections.singleton(skuId));
            //判断购物车详情是否为空
            if (cartItemList == null) {
                throw new RuntimeException("购物车详情为空");
            }

            //遍历购物车详情
            for (SkuInfo skuInfo : cartItemList) {
                //获取购物车详情中的skuId
                Long skuId1 = skuInfo.getSkuId();
                //判断skuId是否为空
                if (skuId1 == null) {
                    throw new RuntimeException("skuId为空");
                }

                //判断skuId是否为空
                if (skuId1.equals(skuId)) {
                    //删除购物车详情
                   redisService.deleteObject(cartKey);
                }
            }

        }
        System.out.println("下单");
    }

}
